package com.yuan.model_community.service.impl;

import com.yuan.model_common.dto.CommonQueryCriteria;
import com.yuan.model_community.domain.CommunityNotice;
import com.yuan.model_community.repository.CommunityNoticeDao;
import com.yuan.model_community.service.CommunityNoticeService;
import com.yuan.model_community.service.dto.CommunityNoticeDto;
import com.yuan.model_community.service.mapstruct.CommunityNoticeMapper;
import lombok.RequiredArgsConstructor;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.utils.PageResult;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;


@Service
@RequiredArgsConstructor
public class CommunityNoticeServiceImpl implements CommunityNoticeService {

    private final CommunityNoticeDao communityNoticeDao;

    private final CommunityNoticeMapper communityNoticeMapper;

    @Override
    public PageResult<CommunityNoticeDto> queryAll(Pageable pageable) {
        Page<CommunityNotice> all = communityNoticeDao.findAll(pageable);
        return PageUtil.toPage(all.map(communityNoticeMapper::toDto));
    }

    @Override
    public List<CommunityNoticeDto> queryAll(CommonQueryCriteria criteria) {
        List<CommunityNotice> users = communityNoticeDao.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder));
        return communityNoticeMapper.toDto(users);
    }

    @Override
    public void download(List<CommunityNoticeDto> queryAll, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (CommunityNoticeDto communityNoticeDto : queryAll) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("公告Id", communityNoticeDto.getNoticeId());
            map.put("公告标题", communityNoticeDto.getNoticeTitle());
            map.put("公告内容", communityNoticeDto.getNoticeText());
            map.put("用户Id", communityNoticeDto.getUserId());
            map.put("发布时间", communityNoticeDto.getNoticeTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(CommunityNotice resources) {
        communityNoticeDao.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(CommunityNotice resources) throws Exception {
        Optional<CommunityNotice> byId = communityNoticeDao.findById(resources.getNoticeId());
        if (byId.isPresent()) {
            CommunityNotice communityNotice = byId.get();
            communityNotice.setNoticeText(resources.getNoticeText());
            communityNotice.setNoticeTitle(resources.getNoticeTitle());
            communityNotice.setNoticeTime(resources.getNoticeTime());
            communityNoticeDao.save(communityNotice);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Integer ids) {
        communityNoticeDao.deleteById(ids);
    }
}
